<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/13
 * Time: 15:00
 */

namespace App\Http\Controllers\HumanResourceManage;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\EmployeeModel;
use App\Http\Model\GroupMembersModel;
use App\Http\Model\GroupModel;
use App\Http\Model\ProjectRosterModel;
use App\Http\Model\WorkmanAccountsModel;
use App\Http\Model\WorkmanAssignmentModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GroupController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function lists(Request $request)
    {
        $rules = [
            'projectId' => 'nullable|integer',
            'professionId' => 'nullable|integer',
            'length' => 'nullable|integer|in:10,20,50',
            'page' => 'nullable|integer|min:1',
        ];
        $message = [
            'projectId.integer' => '项目参数类型错误',
            'professionId.required' => '获取工种参数失败',
            'professionId.integer' => '工种参数类型错误',
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only(['projectId', 'professionId', 'search', 'length', 'page']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $groupModel = new GroupModel();
            $lists = $groupModel->lists($param);
            $countLists = $groupModel->countLists($param);
            $this->data = $this->tableFormat($lists,$countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 430101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function add(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'name' => 'required',
            'professionId' => 'required|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'name.required' => '请填写班组名称',
            'professionId.required' => '请选择工种',
            'professionId.integer' => '工种参数类型错误',
        ];
        $param = $request->only(['projectId', 'name', 'professionId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $groupModel = new GroupModel();
            $info = $groupModel->checkRepeat(['projectId' => $param['projectId'], 'name' => $param['name'], 'professionId' => $param['professionId']]);
            if (!empty($info)) {
                throw new ApiException('相同工种下，班组名称不能重复命名', 430202);
            }
            $groupModel->insert($param);
        } else {
            throw new ApiException($validator->errors()->first(), 430201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 班组详情
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function info(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取班组参数失败',
            'id.integer' => '班组参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $groupModel = new GroupModel();
            $this->data = $groupModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(), 430301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function edit(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'name' => 'required',
            'professionId' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取项目参数失败',
            'id.integer' => '项目参数类型错误',
            'name.required' => '请填写班组名称',
            'professionId.required' => '请选择工种',
            'professionId.integer' => '工种参数类型错误',
        ];
        $param = $request->only(['id', 'name', 'professionId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $groupModel = new GroupModel();
            $info = $groupModel->info(['id' => $param['id']]);
            $info = $groupModel->checkRepeat(['projectId' => $info['projectId'], 'name' => $param['name'], 'professionId' => $param['professionId']], $param['id']);
            if (!empty($info)) {
                throw new ApiException('相同工种下，班组名称不能重复命名', 430401);
            }
            $data = [
                'name' => $param['name'],
                'professionId' => $param['professionId'],
            ];
            $groupModel->update(['id'=>$param['id']],$data);
        } else {
            throw new ApiException($validator->errors()->first(), 430401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function editStatus(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'status' => 'required|integer|in:1,2',
        ];
        $message = [
            'id.required' => '获取班组参数失败',
            'id.integer' => '班组参数类型错误',
            'status.required' => '获取班组状态参数失败',
            'status.integer' => '班组状态参数类型错误',
            'status.in' => '班组状态参数不正确',
        ];
        $param = $request->only(['id', 'status']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $groupModel = new GroupModel();
            $groupModel->update(['id'=>$param['id']],['status'=>$param['status']]);
        } else {
            throw new ApiException($validator->errors()->first(), 430401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function memberLists(Request $request)
    {
        $rules = [
            'groupId' => 'required|integer',
        ];
        $message = [
            'groupId.required' => '获取班组参数失败',
            'groupId.integer' => '班组参数类型错误',
        ];
        $param = $request->only(['groupId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $projectGroupMembersModel = new GroupMembersModel();
            //获取当前存在于班组中的人
            $param['status'] = 1;
            $this->data = $projectGroupMembersModel->lists($param);
        } else {
            throw new ApiException($validator->errors()->first(), 430401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function addMember(Request $request)
    {
        $rules = [
            'employeeId' => 'required|integer',
            'projectId' => 'required|integer',
            'groupId' => 'required|integer',
        ];
        $message = [
            'employeeId.required' => '获取工人参数失败',
            'employeeId.integer' => '工人参数类型错误',
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'groupId.required' => '获取班组参数失败',
            'groupId.integer' => '班组参数类型错误',
        ];
        $param = $request->only(['employeeId', 'groupId','projectId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $groupMembersModel = new GroupMembersModel();
            $result = $groupMembersModel->isInGroup($param);
            if ($result) {
                throw new ApiException('工人已经属于班组' . $result['name'], 430502);
            }
            $info = $groupMembersModel->info(['employeeId'=>$param['employeeId'],'groupId'=>$param['groupId']]);
            if (empty($info)) {
                $groupMembersModel->insert(['employeeId'=>$param['employeeId'],'groupId'=>$param['groupId']]);
            } else {
                if ($info['status'] == 0) {
                    $groupMembersModel->update(['id' => $info['id']], ['status' => 1]);
                }
            }

            $employeeModel = new EmployeeModel();
            $employeeModel->update(['id'=>$param['employeeId']], ['groupId' => $param['groupId']]);
//            TODO  project_roster项目花名册表，当工人所属的班组发生同项目班组变动的时候，应该怎么记录这个数据
//            $projectRosterModel = new ProjectRosterModel();
//            $projectRosterModel->update(['employeeId'=>$param['employeeId'],'projectId'=>$param['projectId']],['groupId'=>$param['groupId']]);
        } else {
            throw new ApiException($validator->errors()->first(), 430501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function delMember(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取班组成员参数失败',
            'id.integer' => '班组成员参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $groupMembersModel = new GroupMembersModel();
            $info = $groupMembersModel->info(['id' => $param['id']]);
            $groupMembersModel->update($param,['status'=>0]);

            $employeeModel = new EmployeeModel();
            $employeeModel->update(['id'=>$info['employeeId']], ['groupId' => null]);

        } else {
            throw new ApiException($validator->errors()->first(), 430601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 删除班组
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function delete(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取班组参数失败',
            'id.integer' => '班组参数类型错误',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $groupModel = new GroupModel();
            $info = (new WorkmanAccountsModel())->info(['groupId' => $param['id']]);
            if (!empty($info)) {
                throw new ApiException('该班组已有班组人员记录分账', 430702);
            }
            //查看班组是否有施工项
            $info = (new WorkmanAssignmentModel())->info(['groupId' => $param['id']]);
            if (!empty($info)) {
                throw new ApiException('该班组已记录施工量价清单', 430703);
            }
            //查看班组下是否有成员
            $info = (new GroupMembersModel())->info(['groupId' => $param['id']]);
            if (!empty($info)) {
                throw new ApiException('该班组已有班组人员', 430704);
            }
            $result = $groupModel->delete($param);
        } else {
            throw new ApiException($validator->errors()->first(), 430701);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function changeProject(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'projectId' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取班组参数失败',
            'id.integer' => '班组参数类型错误',
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
        ];
        $param = $request->only(['id', 'projectId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $groupModel = new GroupModel();
            $info = $groupModel->info(['id'=>$param['id']]);
            if ($info['projectId'] == $param['projectId']){
                throw new ApiException('所选项目与当前项目一致，不能完成此操作', 430801);
            }
            $groupModel->changeProject($param);
        } else {
            throw new ApiException($validator->errors()->first(), 430801);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function selectLists(Request $request)
    {
        $rules = [
            'projectId' => 'nullable|integer',
            'professionId' => 'nullable|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'professionId.required' => '获取工种参数失败',
            'professionId.integer' => '工种参数类型错误',
        ];
        $param = $request->only('projectId','professionId');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
//            $param['status'] = 1;
            $groupModel = new GroupModel();
            $this->data = $groupModel->lists($param);
        } else {
            throw new ApiException($validator->errors()->first(), 430901);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function notInGroup(Request $request){
        $rules = [
            'projectId' => 'required|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
        ];
        $param = $request->only('projectId');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $employeeModel = new EmployeeModel();
            $this->data = $employeeModel->notInGroup($param);
        } else {
            throw new ApiException($validator->errors()->first(), 430901);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}